package com.hanamaki.ecshop.register;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.*;
import java.util.concurrent.TimeUnit;

public class EcshopSeleniumIDERegisterTest {
  private WebDriver driver;
  @BeforeClass
  public void setUp() {
    System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
    driver = new FirefoxDriver();

    /* 隐式等待时间 */
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test(dataProvider = "dp1")
  public void ecshopSeleniumIDERegister(String username, String email, String pwd,
                                        String c_pwd, String msn, String qq, String p1, String p2,
                                        String p3, String p4, String answer) {

    /* 拼接 字符串  */
    StringBuffer buffer = new StringBuffer();
    buffer.append("//option[. = '").append(p4).append("']");

    System.out.println(buffer.toString());

    driver.get("http://192.168.0.129/ecshop/user.php?act=register");
    driver.manage().window().maximize();
    driver.findElement(By.id("username")).sendKeys(username);
    driver.findElement(By.id("email")).sendKeys(email);
    driver.findElement(By.id("password1")).sendKeys(pwd);
    driver.findElement(By.id("conform_password")).sendKeys(c_pwd);
    driver.findElement(By.name("extend_field1")).sendKeys(msn);
    driver.findElement(By.name("extend_field2")).sendKeys(qq);
    driver.findElement(By.name("extend_field4")).sendKeys(p1);
    driver.findElement(By.name("extend_field3")).sendKeys(p2);
    driver.findElement(By.name("extend_field5")).sendKeys(p3);
    {
      WebElement dropdown = driver.findElement(By.name("sel_question"));
      dropdown.findElement(By.xpath(buffer.toString())).click();
    }
    driver.findElement(By.cssSelector("td option:nth-child(2)")).click();
    driver.findElement(By.name("passwd_answer")).sendKeys(answer);
    driver.findElement(By.name("Submit")).click();

    /* 想要在 注册成功之后 做断言判断  需要判断 网页源代码中 是否包含  当前注册的人的 用户名  */
    String pageSource = driver.getPageSource();
    if (pageSource.contains(username)) {
      System.out.println("成功的");
    }

    Assert.assertTrue(pageSource.contains(username));

  }

  @DataProvider
  public Object[][] dp1() {
    return new Object[][]{
            new Object[]{"admin12", "12367@qq.com", "123456", "123456", "123@qq.com", "123456", "11111111111", "11111111111", "11111111111", "我最好朋友的生日？", "1118"},
            new Object[]{"admin23", "12376@qq.com", "123456", "123456", "123@qq.com", "123456", "11111111111", "11111111111", "11111111111", "我最好朋友的生日？", "1118"},
            new Object[]{"admin34", "12893@qq.com", "123456", "123456", "123@qq.com", "123456", "11111111111", "11111111111", "11111111111", "我最好朋友的生日？", "1118"},
            new Object[]{"admin45", "12983@qq.com", "123456", "123456", "123@qq.com", "123456", "11111111111", "11111111111", "11111111111", "我最好朋友的生日？", "1118"},
    };
  }

  @AfterClass
  public void tearDown() {
    driver.quit();
  }
}
